<?php

namespace Admin\Model;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Select;
use Zend\Stdlib\DateTime;
use Zend\Session\Container;


class GalleryTable extends AbstractTableGateway {

    protected $table = 'gallery';

    public function __construct(Adapter $adapter) {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Gallery());

        $this->initialize();
    }

    public function fetchAll(Select $select = null) {
        if (null === $select)
            $select = new Select();
        $select->from($this->table,array('*'))
					->join(
					'nguoidung',
					'gallery.tacgia = nguoidung.id',
					array('hoten'));
        $resultSet = $this->selectWith($select);
        $resultSet->buffer();
        return $resultSet;
    }

    public function getGallery($id) {
        $id = (int) $id;
        $rowset = $this->select(array('id' => $id));
        $row = $rowset->current(); 
        return $row;
    }
	
	public function getLast(Select $select = null) {
        if (null === $select)
            $select = new Select();
        $select->from($this->table,array('*'))->order('id desc')->limit(1);
        $resultSet = $this->selectWith($select);
        $row = $resultSet->current(); 
        return $row;
    }

    public function saveGallery($tengallery,$mota) {
		
		date_default_timezone_set('Asia/Ho_Chi_Minh');
		$str_search = array ( 
		"Mon", 
		"Tue", 
		"Wed", 
		"Thu", 
		"Fri", 
		"Sat", 
		"Sun" 
		); 
		$str_replace = array ( 
		"Thứ hai", 
		"Thứ ba", 
		"Thứ tư", 
		"Thứ năm", 
		"Thứ sáu", 
		"Thứ bảy", 
		"Chủ nhật" 
		);
		$timenow = gmdate("D, d-m-Y", time() + 7*3600); 
		$date = str_replace( $str_search, $str_replace, $timenow); 
		$now = getdate(); 
    	$currentTime = $now["hours"] . ":" . $now["minutes"]; 
		$date = $date." ".$currentTime;
		
		$sessionUser = new Container('user');
		$tacgia = $sessionUser->userid;	
		
        
			$data = array(
				'tengallery' => $tengallery,
				'ngaytao' => $date,
				'ngaycapnhat' => $date,
				'tacgia' => $tacgia,
				'mota' => $mota,
				
			);
            	
                $this->insert($data);
            
        
    }
	public function capnhatGallery($id,$tengallery,$mota) {
		
		date_default_timezone_set('Asia/Ho_Chi_Minh');
		$str_search = array ( 
		"Mon", 
		"Tue", 
		"Wed", 
		"Thu", 
		"Fri", 
		"Sat", 
		"Sun" 
		); 
		$str_replace = array ( 
		"Thứ hai", 
		"Thứ ba", 
		"Thứ tư", 
		"Thứ năm", 
		"Thứ sáu", 
		"Thứ bảy", 
		"Chủ nhật" 
		);
		$timenow = gmdate("D, d-m-Y", time() + 7*3600); 
		$date = str_replace( $str_search, $str_replace, $timenow); 
		$now = getdate(); 
    	$currentTime = $now["hours"] . ":" . $now["minutes"]; 
		$date = $date." ".$currentTime;
		
		
		
        
			$data = array(
				'tengallery' => $tengallery,
				
				'ngaycapnhat' => $date,
				'mota' => $mota,
				
			);
            	
                $this->update($data,array('id' => $id));
            
        
    }

    public function deleteGallery($id) {
        $this->delete(array('id' => $id));
    }
	public function xoaGallery($id) {
		
        $this->delete('id IN (' . $id . ')');
    }
	

	


}
